package com.liu.service;

import com.google.common.collect.Maps;
import com.liu.domain.PrecreatePojo;
import com.liu.domain.UserVo;
import com.liu.util.BeanUtils;
import com.liu.util.CurlUtils;
import com.liu.util.MD5TWOUtil;
import com.liu.common.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class BaiduServiceCurlImpl extends BaiduService {


    public static StringBuffer getData(Map<String ,Object> map){
        StringBuffer params=new StringBuffer();
        for(Map.Entry<String,Object> entry : map.entrySet()){
            log.info(""+entry.getKey() +map.get(entry.getKey()));
            params.append(entry.getKey()+"="+map.get(entry.getKey())+"&");
        }
        return params;


    }


    public static String testFromCurl()throws Exception {
        UserVo user = new UserVo();
        user.setUserName("zhangsan");
        user.setPassword("fdasfads");
        String requestJson= JacksonUtil.toJSon(user);
        log.info(requestJson);

        String address ="http://localhost:9005/curl/test01";

        Map map = BeanUtils.beanToMap(user);
        String params =getData(map).toString();
        params=params.substring(0,params.length()-1);

        log.info("拼装参数"+params);


        String[] cmds = {"curl", "-d",""+params+",",""+address+""};
        String result = CurlUtils.exeCurl(cmds);
        System.out.println(result);
        return result;
    }


    /**
     * 通过curl 的方式进行上传百度云网盘
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadCurl(File file)throws Exception{
        String result="";
//      1.文件预上传
        result=testpreupload(file);
        PrecreatePojo pre =JacksonUtil.readValue(result,PrecreatePojo.class);
        log.info(JacksonUtil.printJson(pre));
//       2.文件分片上传
         result =cutupload(file,pre);
        log.info("分片上传传来的数据："+JacksonUtil.printJson(result));
//        createFile
        result =createFile(file,pre);
        log.info("创建文件传来的数据："+JacksonUtil.printJson(result));

      return result;

    }



    public  String testpreupload(File file)throws  Exception{
        String path="/apps/123/"+file.getName();
        String md5=MD5TWOUtil.calcMD5(file);
//        String aa="curl  -d 'path="+path+"&size="+file.length()+"&isdir=0&autoinit=1&rtype=3&block_list=[\"60bac7b6464d84fed842955e6126826a\"]&content-md5=60bac7b6464d84fed842955e6126826a&slice-md5=3c5c864d432cc2381b687f8d873e1429'";

        Map map = new HashMap();
        map.put("path",path);
        map.put("size",""+file.length()+"");
        map.put("isdir","0");
        map.put("autoinit",1);
        map.put("rtype",3);
        map.put("block_list","[\\\""+md5+"\\\"]");
        map.put("content-md5",md5);
//        map.put("slice-md5",md5);

        String params =getData(map).toString();
        params=params.substring(0,params.length()-1);

        params="\""+params+"\"";
        String address ="\"http://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token="+getToken()+"\"";
        log.info("参数1："+params);
        log.info("地址1："+address);


        String[] cmd ={"curl", "-d",params,  address};
        String result =CurlUtils.exeCurl(cmd);
        System.out.println(result);

        return result;
    }


    public  String cutupload(File file,PrecreatePojo precreatePojo)throws Exception{


        String address="\"https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?access_token="+getToken()+"&method=upload&type=tmpfile&path="+precreatePojo.getPath()+"&uploadid="+precreatePojo.getUploadid()+"&partseq=0\"";

        String params ="\"file=@"+file.getAbsolutePath()+"\"";
        log.info("参数2："+params);
        log.info("地址2："+address);

        String[] cmds = {"curl", "-F",params,address};
        String result =CurlUtils.exeCurl(cmds);
        System.out.println(result);
         return  result;
    }


    public   String  createFile(File file,PrecreatePojo pojo)throws Exception{
        String md5b ="83755cced5529cbb5ee2b864e76f1b0a";
        String path="/apps/123/"+file.getName();
        String aa="curl \"https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=123\" -d '&block_list=[\"7d57c40c9fdb4e4a32d533bee1a4e409\"]' -H \"User-Agent: pan.baidu.com\"";
        String md5=MD5TWOUtil.calcMD5(file);
        String address="\"https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token="+getToken()+"\"";


        Map map = Maps.newHashMap();
        map.put("path",path);
        map.put("size",""+file.length());
        map.put("isdir","0");

        map.put("uploadid",pojo.getUploadid());
        map.put("rtype",3);
        map.put("block_list","[\\\""+md5+"\\\"]");


        String params =getData(map).toString();
        params=params.substring(0,params.length()-1);
        params="\""+params+"\"";

        log.info("参数3："+params);
        log.info("地址3："+address);

        String[] cmds = {"curl","-d",params,address};
        String result =CurlUtils.exeCurl(cmds);
        System.out.println(result);
        return result;



    }

}
